package com.chinalwb.are.demo.slice;

import com.chinalwb.are.ViewScrollListener;
import com.chinalwb.are.demo.ARE_ShowTextAbility;
import com.chinalwb.are.demo.DemoUtil;
import com.chinalwb.are.demo.ResourceTable;
import com.chinalwb.are.styles.ListComponent;
import com.chinalwb.are.utils.FindUtils;
import com.chinalwb.are.utils.ListUtils;
import com.chinalwb.are.utils.NoFieldTextDecorator;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

import static com.chinalwb.are.utils.NoFieldTextDecorator.TYPE_ITALIC;

/**
 * ARE_DefaultToolbarAbilitySlice
 *
 * @since 2021/06/22
 */
public class ARE_DefaultToolbarAbilitySlice extends AbilitySlice
        implements Component.ClickedListener, ViewScrollListener {
    private static final String WARP = "\n";
    private ListComponent listComponent;
    private Image img;
    private boolean isLeft = true;
    private Integer[] icon;
    private final String html = " " + WARP + "In this release, you have a new usage with ARE" + WARP +
            " " + WARP +
            "AREditText + ARE_Toolbar, you are now able to control the position of " +
            "the input area and where to put the toolbar at and,what ToolItems you'd " +
            "like to have in the toolbar." + WARP +
            " " + WARP +
            "You can not only define the Toolbar (and it's style), " +
            "you can also add your own ARE_ToolItem with your style into ARE." + WARP +
            " " + WARP +
            "Fixed app color override bug;edit mode click picture won't open keyboard" + WARP +
            " " + WARP +
            "Why not give it a try now?";

    @Override
    public void onStart(Intent intent) {
        DemoUtil.initStatus(getAbility());
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_are__default_toolbar);
        listComponent = FindUtils.findListComponentById(getAbility(), ResourceTable.Id_list_default);
        img = FindUtils.findImageById(getAbility(), ResourceTable.Id_arrow);
        Text tvContent = FindUtils.findTextById(getAbility(), ResourceTable.Id_tv_name);
        img.setClickedListener(this);
        listComponent.setViewScrollListener(this);
        initList();
        NoFieldTextDecorator
                .decorate(tvContent, html, false)
                .setTextColor(Color.BLUE.getValue(),
                        "In this release, you have a new usage with ARE",
                        "AREditText + ARE_Toolbar, you are now able to control the position of " +
                                "the input area and where to put the toolbar at and,what ToolItems you'd" +
                                " like to have in the toolbar.",
                        "You can not only define the Toolbar (and it's style), " +
                                "you can also add your own ARE_ToolItem with your style into ARE.",
                        "Fixed app color override bug;edit mode click picture won't open keyboard")
                .setFont(TYPE_ITALIC, true, "Why not give it a try now?")
                .setTextColor(Color.getIntColor("#F77EF2"), "Why not give it a try now?")
                .build();
        findComponentById(ResourceTable.Id_showText).setClickedListener(component -> {
            Intent intents = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName("com.chinalwb.are.demo")
                    .withAbilityName(ARE_ShowTextAbility.class.getCanonicalName())
                    .build();
            intents.setOperation(operation);
            intents.setParam(ARE_ShowTextSlice.DEFAULT, "default");
            startAbility(intents);
        });
        findComponentById(ResourceTable.Id_save).setClickedListener(component -> DemoUtil.saveHtml(getAbility(), html));
    }

    private void initList() {
        icon = new Integer[]{
                ResourceTable.Media_my_bold,
                ResourceTable.Media_you,
                ResourceTable.Media_italic,
                ResourceTable.Media_underline,
                ResourceTable.Media_strikethrough,
                ResourceTable.Media_fontsize,
                ResourceTable.Media_foregroundcolor,
                ResourceTable.Media_background,
                ResourceTable.Media_quote,
                ResourceTable.Media_listnumber,
                ResourceTable.Media_listbullet,
                ResourceTable.Media_hr,
                ResourceTable.Media_link,
                ResourceTable.Media_subscript,
                ResourceTable.Media_superscript,
                ResourceTable.Media_alignleft,
                ResourceTable.Media_aligncenter,
                ResourceTable.Media_alignright,
                ResourceTable.Media_image,
                ResourceTable.Media_video,
        };
        listComponent.setData(getContext(), ListUtils.getList(icon, 0, 1, 5, 6));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    @Override
    public void onClick(Component component) {
        if (isLeft) {
            img.setPixelMap(ResourceTable.Media_arrow_right);
            listComponent.scrollPosition(icon.length - 1);
        } else {
            img.setPixelMap(ResourceTable.Media_arrow_left);
            listComponent.scrollPosition(0);
        }
        isLeft = !isLeft;
    }

    @Override
    public void scrolled(int position) {
        img.setPixelMap(position == icon.length - 1 ? ResourceTable.Media_arrow_right :
                ResourceTable.Media_arrow_left);
    }
}
